package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Brand;
import com.ruoyi.system.domain.Spec;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.ComputeDetail;
import com.ruoyi.system.service.IComputeDetailService;

import javax.annotation.Resource;

/**
 * 计算详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-01
 */
@Service
public class ComputeDetailServiceImpl implements IComputeDetailService {
    @Resource
    private ComputeDetailMapper computeDetailMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private SpecMapper specMapper;
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询计算详情
     *
     * @param id 计算详情主键
     * @return 计算详情
     */
    @Override
    public ComputeDetail selectComputeDetailById(Long id) {
        return computeDetailMapper.selectComputeDetailById(id);
    }

    /**
     * 查询计算详情列表
     *
     * @param computeDetail 计算详情
     * @return 计算详情
     */
    @Override
    public List<ComputeDetail> selectComputeDetailList(ComputeDetail computeDetail) {
        List<ComputeDetail> computeDetailList = computeDetailMapper.selectComputeDetailList(computeDetail);
        computeDetailList.forEach(item -> {
            if (item.getTypeId() != null) {
                Type type = typeMapper.selectTypeById(item.getTypeId());
                if (type != null) {
                    item.setTypeName(type.getName());
                }
            }
            if (item.getSpecId() != null) {
                Spec spec = specMapper.selectSpecById(item.getSpecId());
                if (spec != null) {
                    item.setSpecName(spec.getName());
                }
            }
            if (item.getBrandId() != null) {
                Brand brand = brandMapper.selectBrandById(item.getBrandId());
                if (brand != null) {
                    item.setBrandName(brand.getName());
                }
            }
            if (item.getSchoolId() != null) {
                SysDept sysDept = sysDeptMapper.selectDeptById(item.getSchoolId());
                if (sysDept != null) {
                    item.setSchoolName(sysDept.getDeptName());
                }
            }
        });

        return computeDetailList;
    }

    /**
     * 新增计算详情
     *
     * @param computeDetail 计算详情
     * @return 结果
     */
    @Override
    public int insertComputeDetail(ComputeDetail computeDetail) {
        computeDetail.setCreateTime(DateUtils.getNowDate());
        return computeDetailMapper.insertComputeDetail(computeDetail);
    }

    /**
     * 修改计算详情
     *
     * @param computeDetail 计算详情
     * @return 结果
     */
    @Override
    public int updateComputeDetail(ComputeDetail computeDetail) {
        computeDetail.setUpdateTime(DateUtils.getNowDate());
        return computeDetailMapper.updateComputeDetail(computeDetail);
    }

    /**
     * 批量删除计算详情
     *
     * @param ids 需要删除的计算详情主键
     * @return 结果
     */
    @Override
    public int deleteComputeDetailByIds(Long[] ids) {
        return computeDetailMapper.deleteComputeDetailByIds(ids);
    }

    /**
     * 删除计算详情信息
     *
     * @param id 计算详情主键
     * @return 结果
     */
    @Override
    public int deleteComputeDetailById(Long id) {
        return computeDetailMapper.deleteComputeDetailById(id);
    }
}
